﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ventas.Data.Managers;
using Ventas.Data.Entities;
using System.Linq;

namespace Ventas.Data.Test.Data
{
    [TestClass]
    public class UnitTest1
    {
        //
        [TestMethod]
        public void TestCategoryList()
        {
            var lista = CategoryManager.GetList("");
            Assert.IsTrue(lista.Count > 0);
        }
        //
        [TestMethod]
        public void TestAddCategory()
        {
            Category category = new Category()
            {
                Name = "NOMBRE" + DateTime.Now,
                Description = "DESCRIPCION",
            };
            Category categoryDB = CategoryManager.Create(category);
            Assert.IsNotNull(categoryDB.Id);
        }
        //
        [TestMethod]
        public void TestUpdateCategory()
        {
            Category categoryDB = CategoryManager.GetList("").First();
            string Name = categoryDB.Name;
            categoryDB.Name = "HOLA" + DateTime.Now;

            Category r = CategoryManager.Update(categoryDB);
            Assert.AreNotEqual(Name, r.Name);
        }
        //
        [TestMethod]
        public void TestGetByIdCategory()
        {
            Guid categoryId = CategoryManager.GetList("").First().Id;
            Category r = CategoryManager.GetById(categoryId);
            Assert.IsNotNull(r.Name);
        }
        //
        [TestMethod]
        [ExpectedException(typeof(Exception))]
        public void TestGetByIdCategoryException()
        {
            Guid categoryId = new Guid("15972402-76B0-E411-BE67-74DE2B991954");
            Category r = CategoryManager.GetById(categoryId);
        }
        //
        [TestMethod]
        public void TestDeleteCategory()
        {
            Guid categoryId = CategoryManager.GetList("").First().Id;
            CategoryManager.Delete(categoryId);
            Category r = GetById(categoryId);
            Assert.IsNull(r);
        }
        //
        private Category GetById(Guid Id)
        {
            using (VentasDbContext ctx = new VentasDbContext())
            {
                return ctx.Categorys.Find(Id);
            }
        }
    }
}
